package com.miao.common.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;

/**
 * @author MiaoWenqing
 * @description: jwt生成token 与解析token类
 * @date 2021/1/16 15:59
 */
public class JWTUtil {


    private static final String Sign="h%a8!2.s9fs(uz*d/sc)i7^4:du0";

    public static String createToken(String json){
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, 1);
        String token = JWT.create()
                .withExpiresAt(instance.getTime())
                .withClaim("data", json)
                .sign(Algorithm.HMAC256(Sign));


        return token;
    }

    public static String analysisToken(String token) throws JWTDecodeException {
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(Sign)).build();
        DecodedJWT verify=jwtVerifier.verify(token);


       return verify.getClaim("data").asString();

    }

}
